<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\Test;
use App\Service\QueueService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;

/**
 * Class IndexController
 * @AutoController()
 * @package App\Controller
 */
class IndexController extends AbstractController
{

    /**
     * 队列service
     * @Inject
     * @var QueueService
     */
    private $QueueService;

    /**
     * @return array
     */
    public function index(): array
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();

        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }

    public function gonggao()
    {
        $close = $this->request->input('close');
        $websocket = websocket();
        foreach ($websocket->connections as $fd)
        {
            $info = $websocket->getClientInfo($fd);
            //过滤掉非websocket链接
            if ($websocket->exist($fd) && $info['websocket_status'] > 0)  {
                //用户信息
                if ($info['uid'] && redis()->exists('user_'.$info['uid'])) {
                    var_dump(redis()->hGetAll('user_'.$info['uid']));
                }
                if ($close) {
                    $websocket->push($fd, "被关闭！".time());
                    $websocket->close($fd);
                } else {
                    $websocket->push($fd, "这是一个公告".time());
                }
            }
        }

        return 'ok';
    }
}
